using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
using System.Text;

namespace SDK.Scripts.Utils
{
	public class FileUtil
	{
		public static bool MoveTo(string srcPath, string desPath)
		{
			if (File.Exists(srcPath))
			{
				string directoryName = Path.GetDirectoryName(desPath);
				if (!Directory.Exists(directoryName))
				{
					Directory.CreateDirectory(directoryName);
				}
				if (File.Exists(desPath))
				{
					File.Delete(desPath);
				}
				try
				{
					FileInfo fileInfo = new FileInfo(srcPath);
					fileInfo.MoveTo(desPath);
				}
				catch (Exception ex)
				{
					Debuger.LogError("errpr " + ex.Message);
					return false;
				}
				return true;
			}
			return false;
		}

		public static bool CopyTo(string srcPath, string desPath, bool overwrite = false)
		{
			if (File.Exists(srcPath))
			{
				string directoryName = Path.GetDirectoryName(desPath);
				if (!Directory.Exists(directoryName))
				{
					Directory.CreateDirectory(directoryName);
				}
				if (overwrite && File.Exists(desPath))
				{
					File.Delete(desPath);
				}
				if (!File.Exists(desPath))
				{
					try
					{
						FileInfo fileInfo = new FileInfo(srcPath);
						fileInfo.CopyTo(desPath, overwrite: true);
					}
					catch (Exception ex)
					{
						Debuger.LogError("errpr " + ex.Message);
						return false;
					}
				}
				return true;
			}
			return false;
		}

		public static bool CopyToByDirectory(string srcPath, string desPath, bool overwrite = false, string postfixs = "", char split = '#')
		{
			if (!Directory.Exists(srcPath))
			{
				Debuger.LogError("源文件夹不存在");
				return false;
			}
			string[] array = postfixs.Split(split);
			string[] files = Directory.GetFiles(srcPath);
			foreach (string text in files)
			{
				if (array.Length > 0)
				{
					foreach (string value in array)
					{
						if (text.EndsWith(value))
						{
							string desPath2 = desPath + Path.GetFileName(text);
							CopyTo(text, desPath2, overwrite);
							break;
						}
					}
				}
				else
				{
					string desPath3 = desPath + Path.GetFileName(text);
					CopyTo(text, desPath3, overwrite);
				}
			}
			return true;
		}

		public static string ReadFile(string path, bool writeBom = true)
		{
			try
			{
				FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
				byte[] array = new byte[fileStream.Length];
				fileStream.Read(array, 0, array.Length);
				string empty = string.Empty;
				empty = ((!writeBom) ? Encoding.UTF8.GetString(array, 0, array.Length) : Encoding.UTF8.GetString(array, 3, array.Length - 3));
				fileStream.Dispose();
				return empty;
			}
			catch (IOException ex)
			{
				throw new Exception("ReadFile Exception : readfile faild :" + ex.Message);
			}
		}

		public static byte[] FileStreamReadFile(string path)
		{
			FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
			byte[] array = new byte[fileStream.Length];
			fileStream.Read(array, 0, array.Length);
			fileStream.Close();
			fileStream.Dispose();
			return array;
		}

		public static bool WriteToFileAppend(string path, string content)
		{
			if (File.Exists(path))
			{
				try
				{
					byte[] bytes = Encoding.UTF8.GetBytes(content);
					FileStream fileStream = new FileStream(path, FileMode.Append, FileAccess.Write);
					fileStream.Write(bytes, 0, bytes.Length);
					fileStream.Close();
					fileStream.Dispose();
					return true;
				}
				catch (IOException ex)
				{
					Debuger.LogError("write file faild = " + ex.Message);
					return false;
				}
			}
			return false;
		}

		public static void WriteToFile(string path, string content)
		{
			if (File.Exists(path))
			{
				try
				{
					byte[] bytes = Encoding.UTF8.GetBytes(content);
					WriteToFile(path, bytes);
				}
				catch (IOException ex)
				{
					Debuger.LogError("write file faild = " + ex.Message);
				}
			}
		}

		public static void WriteToFile(string path, byte[] bytes)
		{
			if (File.Exists(path))
			{
				try
				{
					FileStream fileStream = new FileStream(path, FileMode.Truncate, FileAccess.Write);
					fileStream.Write(bytes, 0, bytes.Length);
					fileStream.Close();
					fileStream.Dispose();
				}
				catch (IOException ex)
				{
					Debuger.LogError("append file faild = " + ex.Message);
				}
			}
			else
			{
				try
				{
					FileStream fileStream2 = new FileStream(path, FileMode.Create);
					fileStream2.Write(bytes, 0, bytes.Length);
					fileStream2.Dispose();
				}
				catch (IOException ex2)
				{
					throw new Exception("writeToFile Exception " + ex2.Message);
				}
			}
		}

		public static void DeleteFile(string fullLocalPath)
		{
			Common.SafeCall(delegate
			{
				if (File.Exists(fullLocalPath))
				{
					File.Delete(fullLocalPath);
				}
			});
		}

		public static void DeleteDire(string fullLocalPath, bool recursive = true)
		{
			Common.SafeCall(delegate
			{
				if (Directory.Exists(fullLocalPath))
				{
					Directory.Delete(fullLocalPath, recursive);
				}
			});
		}

		public static string GetUTF8String(byte[] buffer)
		{
			if (HasBom(buffer))
			{
				return new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetString(buffer, 3, buffer.Length - 3);
			}
			return Encoding.UTF8.GetString(buffer);
		}

		public static bool HasBom(byte[] buffer)
		{
			if (buffer == null)
			{
				return false;
			}
			if (buffer.Length <= 3)
			{
				return false;
			}
			byte[] array = new byte[3]
			{
				239,
				187,
				191
			};
			if (buffer[0] == array[0] && buffer[1] == array[1] && buffer[2] == array[2])
			{
				return true;
			}
			return false;
		}

		public static void UnZipFile(string zipFilePath, string unZipDir, string dir = "")
		{
			using (ZipInputStream zipInputStream = new ZipInputStream(File.OpenRead(zipFilePath)))
			{
				ZipEntry nextEntry;
				while ((nextEntry = zipInputStream.GetNextEntry()) != null)
				{
					string directoryName = Path.GetDirectoryName(nextEntry.Name);
					if (directoryName.Length > 0 && (dir.Equals(string.Empty) || directoryName.Contains(dir)))
					{
						if (!Directory.Exists(unZipDir + directoryName))
						{
							Directory.CreateDirectory(unZipDir + directoryName);
						}
						string fileName = Path.GetFileName(nextEntry.Name);
						if (fileName != string.Empty)
						{
							using (FileStream fileStream = File.Create(unZipDir + nextEntry.Name))
							{
								int num = 2048;
								byte[] array = new byte[2048];
								while (true)
								{
									num = zipInputStream.Read(array, 0, array.Length);
									if (num <= 0)
									{
										break;
									}
									fileStream.Write(array, 0, num);
								}
							}
						}
					}
				}
			}
		}

		public static string GetLastDirectoryPath(string path)
		{
			int num = path.LastIndexOf(Path.AltDirectorySeparatorChar);
			int num2 = path.LastIndexOf(Path.DirectorySeparatorChar);
			int num3 = (num >= num2) ? num : num2;
			return path.Substring(num3 + 1);
		}
	}
}
